//
//  ViewController.swift
//  Calculator
//
//  Created by mqm on 16/4/22.
//  Copyright © 2016年 mqm. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

					
    @IBOutlet weak var display: UILabel!
    
    var flag = false
    var brain = CalculatorBrain()
    
    @IBAction func appendDigit(sender: UIButton) {
        let digit = sender.currentTitle!
        
        if flag{
            display.text = display.text! + digit
        }else{
            display.text = digit
            flag = true
        }
        
    }
    @IBAction func operate(sender: UIButton) {
        
        if flag{
            enter()
        }
        if let operation = sender.currentTitle{
            if let result = brain.performOperation(operation)
            {
                displayValue = result
            } else {
                displayValue = 0
            }
        }
    }
        

    @IBAction func result(sender: AnyObject) {
        print("value:\(operandStack.description)")

        operandStack = Array<Double>()
    }
    
    var operandStack = Array<Double>()
    
    @IBAction func enter() {
        flag = false
        if let result = brain.pushOperand(displayValue){
            displayValue = result
        } else {
            displayValue = 0 //如果出错了那么暂且设为0
        }
    }
    
    
    var displayValue:Double{
        get{
            return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
        }
        set{
            display.text = "\(newValue)"
            flag = false
        }
    }
}

